//
//  CJBZSplashAdapter.m
//  CJModileBeiZiAdapter
//
//  Created by shanghailingye on 2025/10/20.
//

#import "CJBZSplashAdapter.h"

@interface CJBZSplashAdapter()<CJSplashAdDelegate>

@property (nonatomic, assign) BOOL isBiddingRequest;
@property (nonatomic, strong) CJSplashAd *splashAd;
@property (nonatomic, strong) AMPSAdConfiguration *adConfiguration;

@end
@implementation CJBZSplashAdapter

/// 竟胜回传
- (void)sendWinNotificationWithInfo:(NSDictionary *)winInfo {
    
}

/// 竞负回传
- (void)sendLossNotificationWithInfo:(NSDictionary *)lossInfo {
    
}

- (void)requestBiddingPriceWithSpaceId:(NSString *)spaceId
                       adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.isBiddingRequest = YES;
    [self loadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
}

- (void)loadAdWithSpaceId:(NSString *)spaceId adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    if (CJBZSDKManagerAdapter.initState != kAdScopeMediationAdapterInitSDKStateSuccess) {
        __weak typeof(self)weakSelf = self;
        [CJBZSDKManagerAdapter startAsyncWithAppId:self.appId configuration:self.sdkConfiguration results:^(BOOL adapterResult) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (adapterResult) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [strongSelf lingYeLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
                });
            } else {
                if (strongSelf.isBiddingRequest) {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSGDTSDKManagerAdapter init fail"}];
                    if (strongSelf.bidDelegate && [strongSelf.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
                        [strongSelf.bidDelegate requestBiddingPriceFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                } else {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:270401 userInfo:@{@"NSLocalizedDescription": @"AMPSGDTSDKManagerAdapter init fail"}];
                    if (strongSelf.customDelegate && [strongSelf.customDelegate respondsToSelector:@selector(splashAdLoadFail:error:ext:)]) {
                        [strongSelf.customDelegate splashAdLoadFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                }
            }
        }];
    } else {
        [self lingYeLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
    }
}

- (void)lingYeLoadAdWithSpaceId:(NSString *)spaceId adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.splashAd = [[CJSplashAd alloc] initWithResourceId:spaceId rootViewController:adConfiguration.viewController customLogoView:adConfiguration.bottomView];
    self.splashAd.delegate = self;
    [self.splashAd loadAdData];
}

- (void)loadC2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    if (self.isLoadAdSuccess) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdLoadSuccess:ext:)]) {
            [self.customDelegate splashAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
    }
}

- (void)loadS2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    
}

- (NSInteger)eCPM {
    return [self.splashAd getECpm];
}

- (BOOL)isValid {
    return self.isLoadAdSuccess;
}

- (void)showSplashViewInWindow:(nonnull UIWindow *)window {
    [self.splashAd showSplashAdToWindow:window];
}

#pragma mark CJSplashAdDelegate
- (void)splashAdDidLoad:(CJSplashAd *)splashAd resourceId:(NSString *)resourceId {
    self.isLoadAdSuccess = true;
    if (!self.isBiddingRequest) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdLoadSuccess:ext:)]) {
            [self.customDelegate splashAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
        return;
    }
    if ([splashAd getECpm] > 0) {
        if (self.bidDelegate && [self.bidDelegate  respondsToSelector:@selector(requestBiddingPriceSuccess:ext:)]) {
            [self.bidDelegate requestBiddingPriceSuccess:self ext:self.adConfiguration.ext];
        }
    } else {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
            NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"CJBZSplashAdapter ad bidding fail,ecpm less than 0"}];
            [self.bidDelegate requestBiddingPriceFail:self error:error ext:self.adConfiguration.ext];
        }
    }
}

- (void)splashAdOnShow:(CJSplashAd *)splashAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdExposured:)]) {
        [self.customDelegate splashAdExposured:self];
    }
}

- (void)splashAdLoadFailed:(CJSplashAd *)splashAd error:(NSError *)error {
    if (self.isBiddingRequest) {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
            [self.bidDelegate requestBiddingPriceFail:self error:error ext:self.adConfiguration.ext];
        }
    } else {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdLoadFail:error:ext:)]) {
            [self.customDelegate splashAdLoadFail:self error:error ext:self.adConfiguration.ext];
        }
    }
    [self removeAd];
}

- (void)splashAdOnClicked:(CJSplashAd *)splashAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdDidClick:ext:)]) {
        [self.customDelegate splashAdDidClick:self ext:self.adConfiguration.ext];
    }
}

- (void)splashAdOnClosed:(CJSplashAd *)splashAd {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(splashAdDidClose:)]) {
        [self.customDelegate splashAdDidClose:self];
    }
    [self removeAd];
}

- (void)removeAd {
    if (_splashAd) {
        _splashAd.delegate = nil;
        _splashAd = nil;
    }
}

- (void)dealloc {
    [self removeAd];
}


@end
